<?php

namespace App\Models;

use App\ModelFilters\AdminFilter;
use EloquentFilter\Filterable;
use Tymon\JWTAuth\Contracts\JWTSubject;
use \Illuminate\Foundation\Auth\User as Authenticatable;

/**
 * App\Models\Admin
 *
 * @property int $id 主键
 * @property string $account 账户名称
 * @property string $password 账户密码
 * @property string $mobile 手机号码
 * @property string $email 邮箱
 * @property string $post 职务
 * @property int $role 账户角色
 * @property int $state 账户状态 1:启用, 2:禁用
 * @property string $login_ip 最后登录IP
 * @property string|null $login_time 最后登录时间
 * @property \Illuminate\Support\Carbon|null $created_at
 * @property \Illuminate\Support\Carbon|null $updated_at
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Admin newModelQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Admin newQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Admin query()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Admin whereAccount($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Admin whereCreatedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Admin whereEmail($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Admin whereId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Admin whereLoginIp($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Admin whereLoginTime($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Admin whereMobile($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Admin wherePassword($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Admin wherePost($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Admin whereRole($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Admin whereState($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Admin whereUpdatedAt($value)
 * @mixin \Eloquent
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Admin filter($input = array(), $filter = null)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Admin paginateFilter($perPage = null, $columns = array(), $pageName = 'page', $page = null)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Admin simplePaginateFilter($perPage = null, $columns = array(), $pageName = 'page', $page = null)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Admin whereBeginsWith($column, $value, $boolean = 'and')
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Admin whereEndsWith($column, $value, $boolean = 'and')
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Admin whereLike($column, $value, $boolean = 'and')
 * @property-read \App\Models\AdminRole $roles
 */
class Admin extends Authenticatable implements JWTSubject
{
    const SUPER_ADMIN_ACCOUNT = 'Admin'; //初始化超级管理员账号

    use Filterable;

    /**
     * 定义默认模型过滤器
     *
     * @return string
     */
    public function modelFilter()
    {
        return $this->provideFilter(AdminFilter::class);
    }

    /**
     * 定义不允许自动填充的字段
     * @var array
     */
    protected $guarded = [
        'id',
        'account',
    ];


    /**
     * 定义不显示出来的字段
     * @var array
     */
    protected $hidden = [
        'password',
    ];

    /**
     * 反向关联角色model
     */
    public function roles()
    {
        return $this->belongsTo('App\Models\AdminRole', 'role', 'id');
    }

    /**
     * Get the identifier that will be stored in the subject claim of the JWT.
     *
     * @return mixed
     */
    public function getJWTIdentifier()
    {
        return $this->getKey();
    }

    /**
     * Return a key value array, containing any custom claims to be added to the JWT.
     *
     * @return array
     */
    public function getJWTCustomClaims()
    {
        return [

        ];
    }

    /**
     * 密码修改器
     *
     * @param $value
     */
    public function setPasswordAttribute($value)
    {
        $this->attributes['password'] = bcrypt($value);
    }

    /**
     * 获取列表
     *
     * @param array $params
     * @param int $page_size
     * @return \Illuminate\Database\Eloquent\Collection|static[]
     */
    public static function getList($params, $page_size = 0)
    {
        $order_field = 'created_at';
        $order_sort = 'desc';
        if (0 === $page_size) {
            $list = self::filter($params)
                ->with('roles')
                ->orderBy($order_field,$order_sort)
                ->get();
            $total = (int)$list->count();
            $result = [
                'list' => $list,
                'total' => $total,
            ];
        } else {
            $list = self::filter($params)
                ->with('roles')
                ->orderBy($order_field, $order_sort)
                ->paginate($page_size);
            $temp = $list->toArray();
            $result = [
                'list' => $temp['data'],
                'total' => $temp['total'],
            ];
        }
        return $result;
    }
}
